<template>
    <div class="blogadd">
      <ul>
        <li>
          <span>标题：</span>
          <input type="text" v-model="title" class="title_input">
        </li>
        <li>
          <span>内容：</span>
          <wangeditor @getcontent="getcontent"></wangeditor>
        </li>
      </ul>
      <el-button type="success" class="but" @click="setContent">发布文章</el-button>
    </div>
</template>

<script>
import wangeditor from '../wangEnduit/wangEnduit.vue'
export default {
  
    data () {
      return {
        title:"",
        content:"",
        setContentUrl:this.pathUrl.setContentUrl,
        // setContentUrl:"http://localhost:3003/api/blog/add",
        user:"",//用户
      }
    },

    methods: {
      /**
       * 成功提示
       */
      open2(message) {
        this.$message({
          showClose: true,
          message,
          type: 'success',
          duration:1500,
        });
      },  
      /**
       * 错误提示
       */         
      open4(message) {
        this.$message({
          showClose: true,
          message: message,
          type: 'error',
          duration:1500,
        });
      },
      /**
       * 获取内容
       */
      getcontent(val){
        this.content=val
        console.log(this.content)
      },
      /**
       * 获取用户信息
       */
      getUser(){
        this.user=JSON.parse(localStorage.getItem("user"))
      },
      /**
       * 内容上传
       */
      setContent(){
        if(this.title==""){
          this.open4("请输入标题")
          return
        }
        else if(this.content==""){
          this.open4("请输入内容")
          return
        }
        //开始增加内容
        this.$axios.post(this.setContentUrl,{
          loginId:this.user.loginId,
          title:this.title,
          content:this.content
        })
        .then((result) => {
          if(result.data.status=="yes"){
            this.open2(result.data.msg)
            this.$router.push("/blog")
          }
          else(
            this.open4("成功")
          )
          
        })
        .catch((err) => {
          console.log(err)
        });
      }

    },
    mounted:function(){
      this.getUser();
    },
    components: {
      wangeditor
    },
}
</script>

<style scoped>
.blogadd{
  padding: 0 10%;
}
.blogadd>li{
  text-align: left;
}
.but{
  display:block;margin:10px auto
}
.title_input{
  width: 80%;
}
</style>